using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Text;

namespace Framework.Library.Utilities
{
	public class ObjectHelper
	{
		public static object CreateObject(Type type, IDataReader dr)
		{
			object objTarget = null;
			try
			{
				if (dr.Read())
				{
					objTarget = Activator.CreateInstance(type);
					PropertyInfo[] objProperties = type.GetProperties();
					for (int i = 0; i < objProperties.Length; i++)
					{
						PropertyInfo property = objProperties[i];
						if (property.CanWrite)
						{
                            if (dr[property.Name] != null)
                            {
                                property.SetValue(objTarget, dr[property.Name], null);
                            }
                            else 
                            {
                                property.SetValue(objTarget, "", null);
                            }
						}
					}

				}
			}finally
			{
				dr.Close();
			}
			return objTarget;
		}
	}
}
